# 0.导包
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score,precision_score,recall_score,f1_score,classification_report,roc_auc_score

# 1.获取数据
data_df =pd.read_csv('churn.csv')
# data_df.info()
# print(data_df )

# 2.数据处理
# 2.1 类别型数据进行热编码
data_df=pd.get_dummies(data_df,dtype=np.uint8)
# data_df.info()
# print(data_df.iloc[:5,13:])
# 2.2 去除热编码意义重复的列
data_df.drop(['Churn_No','gender_Male'],axis=1,inplace=True)
# data_df.info()
# print(data_df.iloc[:5,13:])

# 2.3 修改列名
data_df.rename(columns={'Churn_Yes':'label','gender_Female':'gender'},inplace=True)
# data_df.info()

# 2.4 样本均衡
print(data_df.label.value_counts(1))

# 3.特征工程
# 3.1 特征筛选
# sns.countplot(data=data_df,y='gender',hue = 'label')
# plt.show()
x = data_df[['Contract_Month','PaymentElectronic','Partner_att']]
y = data_df['label']

# 3.2 数据划分
x_train,x_test,y_train,y_test =train_test_split(x,y,test_size=0.3,random_state=22)

# 4.模型训练
# LR =LogisticRegression()
LR =LogisticRegression(class_weight='balanced')
LR.fit(x_train,y_train)

# 5.模型预测评估
y_predict =LR.predict(x_test)
print(y_predict)

# 准确率
print('*'*80)
print(accuracy_score(y_predict, y_test))
print(LR.score(x_test, y_test))
# 精确率
print('*'*80)
print(precision_score(y_pred=y_predict, y_true=y_test))
# 召回率
print('*'*80)
print(recall_score(y_pred=y_predict, y_true=y_test))
# f1_score
print('*'*80)
print(f1_score(y_pred=y_predict, y_true=y_test))

# AUC
print('*'*80)
print(recall_score(y_pred=y_predict, y_true=y_test))

# 分类评估报告
print('*'*80)
print(classification_report(y_pred=y_predict, y_true=y_test))